<?php
/**
 * Created by PhpStorm.
 * User: Reborn
 * Date: 2019/12/5
 * Time: 10:38
 */

namespace app;


use think\Model;
use think\model\concern\SoftDelete;

abstract class BaseModel extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = true;
    protected $defaultSoftDelete = 0;
    public $sortBy = 'id';
    public $sortOrder = 'desc';

    /**
     * 获取所有.
     */
    public function all()
    {
        return $this->order($this->sortBy, $this->sortOrder)->select();
    }

    /**
     * 获取分页.
     * User Reborn
     * Time 2020/4/9 14:53
     * @param $paginate
     * @param array $where
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function paginates($paginate,$where=[])
    {
        return $this->where($where)->order($this->sortBy, $this->sortOrder)->paginate($paginate);
    }


    /**
     * 添加一条记录.
     * User Reborn
     * Time 2020/4/9 14:52
     * @param $input
     * @return $this
     */
    public function add($input)
    {
        $this->save($input);
        return $this;
    }

    /**
     * 删除一条记录.
     * User Reborn
     * Time 2020/4/9 14:52
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function remove($id)
    {
        return $this->find($id)->delete();
    }

    /**
     * 更新一条记录
     * User Reborn
     * Time 2020/4/8 14:02
     * @param $id
     * @param array $input
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renew($id, array $input)
    {
        $model = $this->find($id);
        $model->save($input);

        return $model;
    }
}